$(function() {
	
	var msgInfo = "<span class='msgInfo'><p>:msg:</p></span>";
	var msgError = "<span class='msgError'><p>:msg:</p></span>";
	
	$("a[id^=notetitle-]").click(function() {
		var id = $(this).attr('id');
		id = id.split('-')[1];
		$('#notetext-'+id).fadeToggle();
	});
	
	$("input[id^=editbtn-]").click(function() {
		var id = $(this).attr('id');
		id = id.split('-')[1];
		$('#notetext-'+id).fadeOut();
		$('#editnote-'+id).fadeIn();
	});
	
	$("input[id^=cancel-]").click(function() {
		var id = $(this).attr('id');
		id = id.split('-')[1];
		$('#editnote-'+id).fadeOut();
		$('#notetext-'+id).fadeIn();
	});
	
	$("input[id^=save-]").click(function() {
		var id = $(this).attr('id');
		id = id.split('-')[1];
		var text = $('#txtnote-'+id).val();
		text = encodeURIComponent(text);
		
		window.location = "/update?id="+id+"&text="+text;
		
		/*var data = {'id': id, 'text': text};
		
		$.ajax({
			type: "GET",
			url: "/update",
			data: data,
			success: function(data) {
				var json = $.parseJSON(data);
				$('#editnote-'+json.id).fadeOut();
				$('#notetext-'+json.id).fadeIn();
				if (json.error == 0) {
					alert("Note Saved Successfully!");
					$('textarea[id=txtnote-'+json.id+']').val(text);
					var msg = "Note saved successfully!";
					var msgWrapper = msgInfo;
					msgWrapper.replace(':msg:', msg);
					$('li-'+id).append(msgWrapper);
					setTimeout("$('li-"+json.id+" .msgInfo').fadeOut().remove()", 5000);
				}
				else {
					alert("Error "+json.error+" : "+json.msg);
					var msgWrapper = msgError;
					msgWrapper.replace(':msg:', json.msg);
					$('li-'+id).append(msgWrapper);
					setTimeout("$('li-"+json.id+" .msgInfo').fadeOut().remove()", 5000);
				}
			}
		});*/
	});
});